/**
 * Created by feifan on 2016/6/30.
 */
var express = require('express');
var fs=require('fs');

var router = express.Router();

router.post('/', (req, res)=> {
    //log(req.body);
    if(req.session.auth!=1) return res.redirect('/login');
    var article={};
    article.id=db.get('articles').sortBy('id').last().value().id+1;
    article.title=req.body.title;
    article.type=req.body.type;
    article.tag=makeArray(req.body.tag);
    article.category=makeArray(req.body.category,'uncategorized');
    let date=new Date();
    article.pic=req.body.pic;
    article.date=date.toLocaleDateString();
    //log(article)
    db.insert('articles',article);
    fs.writeFile('./articles/'+article.id+'.html', req.body.content);
    res.send(req.body.content);
});

module.exports = router;